package cn.itsource.basic.utils;

import com.alibaba.fastjson.JSONObject;
import okhttp3.*; //minio自带的
import java.io.IOException;
import java.net.URLEncoder;

public class BaiduAiUtils {

    static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().build();
    static final String API_Key = "28de6Nsj65UfhpEL2XCoQP6g";
    static final String Secret_Key = "TgmAskuoEytrX1IrgKSmR134Q8O1xvCf";
    public static String getAccessToken(){
        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, "");
        Request request = new Request.Builder()
                .url("https://aip.baidubce.com/oauth/2.0/token?client_id="
                        +API_Key+"&client_secret="+Secret_Key+"&grant_type=client_credentials")
                .method("POST", body)
                .addHeader("Content-Type", "application/json")
                .addHeader("Accept", "application/json")
                .build();
        Response response = null;
        try {
            response = HTTP_CLIENT.newCall(request).execute();
            String result = response.body().string();

            JSONObject jsonObject = JSONObject.parseObject(result);
            return jsonObject.getString("access_token");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static Boolean textCensor(String content) {
        String url = "https://aip.baidubce.com/rest/2.0/solution/v1/text_censor/v2/user_defined";
        try {
            String param = "text=" + URLEncoder.encode(content, "utf-8");
            // 注意这里仅为了简化编码每一次请求都去获取access_token，线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
            String result = HttpUtil.post(url, getAccessToken(), param);

            JSONObject jsonObject = JSONObject.parseObject(result);
            String conclusion = jsonObject.getString("conclusion");
            if("合规".equals(conclusion)){
                return true; //合规
            }else{
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    //http://60.204.187.34:8800/group1/M00/00/00/wKgAcGY57JmADT4iAAEIR-Nc3Pc429.jpg
    public static Boolean imgCensor(String imgUrl) {
        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/solution/v1/img_censor/v2/user_defined";
        try {
            String param = "imgUrl=" + imgUrl;
            // 注意这里仅为了简化编码每一次请求都去获取access_token，线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
            String result = HttpUtil.post(url, getAccessToken(), param);

            JSONObject jsonObject = JSONObject.parseObject(result);
            String conclusion = jsonObject.getString("conclusion");
            if("合规".equals(conclusion)){
                return true;
            }else{
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 如果imgUrls=null或"" - 不审核
     * 如果imgUrls只有一张图片 - ？
     * 多张图片如何审核 - ？【多张图片格式用,拼接【地址是全地址】】
     * http:123.23.232.1:8099/xx.png,http:123.23.232.1:8099/yy.png,http:123.23.232.1:8099/zz.png
     * @param imgUrls
     * @return
     */
    public static Boolean imgCensors(String imgUrls){
        if(imgUrls==null || imgUrls.trim().equals("")){
            return true;
        }

        String[] urlArr = imgUrls.split(",");
        for (String url : urlArr) {
            if(!imgCensor(url)){
                return false;
            }
        }

        return true;
    }

    public static void main(String[] args) {
        System.out.println(BaiduAiUtils.textCensor("shabi"));
    }
}
